<?php
class Category extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('category_model','category');
	}
	
	function index()
	{
		if(!empty($_GET['search']))
		{
			$this->category->where('name like \'%'.$_GET['search'].'%\'');
		}
		$data['result'] = $this->category->get();
		$data['pagination'] = $this->category->pagination();
		$this->template->build('category_index',$data);
	}
	
	function form($id = NULL)
	{
		$data['rs'] = $this->category->get_row($id);
		$this->template->build('category_form',$data);
	}
	
	function save()
	{
		if($_POST)
		{
			if(empty($_POST['id']))
			{
				add_log('เพิ่มหมวดบัญชี "'.$_POST['name'].'"');
			}
			else
			{
				add_log('แก้ไขหมวดบัญชี "'.$_POST['name'].'"');
			}
			$this->category->save($_POST);
			set_notify('success', lang('save_data_complete'));
		}
		redirect('account/category');
	}
	
	function delete($id)
	{
		$rs = $this->category->get_row($id);
		add_log('ลบหมวดบัญชี "'.$rs['name'].'"');
		$this->category->delete($id);
		set_notify('success', lang('delete_data_complete'));
		redirect($_SERVER['HTTP_REFERER']);
	}
	
}
?>